home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 101-125 / scopedisk120 / mrcat / mrcat.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  4KB  |  141 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #ifdef ARP
  5. #include <ArpFileScan.h>
  6. #include <libraries/arpbase.h>
  7. #else
  8. #include <FileScan.h>
  9. #endif
  10. #include <functions.h>
  11.  
  12. /*  mrcat - catenate files with wildcard expansion.
  13.  *  This program copies a series of files to standard output. It uses smart file
  14.  *  expansion with sorting. Each filename parameter can be thought of as representing
  15.  *  a group of one or more files. Filenames within a group are sorted, but the
  16.  *  original file specifications are not. For example:
  17.  *
  18.  *      mrcat >myfile c a b* d*
  19.  *
  20.  *  would result in
  21.  *
  22.  *      1.  copy file c
  23.  *      2.  copy file a
  24.  *      3.  sort all filenames beginning with b, then copy them
  25.  *      4.  sort all filenames beginning with d, then copy them
  26.  *
  27.  *  If no input files are specified, standard input is copied to standard output.
  28.  */
  29.  
  30. #ifndef ARP
  31. #define Printf  printf
  32. #define Puts    puts
  33. #define SPrintf sprintf
  34. #endif
  35.  
  36. #define BUFSIZE     32*1024             /* 32K copy buffer size */
  37. #ifdef ARP
  38. struct ArpBase  *ArpBase;
  39. #endif
  40. BPTR            myOutput;
  41. char            *buffer;
  42. char            errMsg[256];
  43. char            *progName;
  44.  
  45. void
  46. Abort(const char *errDesc, long fault)
  47. {
  48.     if (IsInteractive(myOutput)) {
  49.         Write(myOutput,progName, strlen(progName));
  50.         Write(myOutput,": ", 2L);
  51.         Write(myOutput,errDesc, strlen(errDesc));
  52.     }
  53. #ifdef ARP
  54.     ArpExit(20, fault);
  55. #else
  56.     exit(1);
  57. #endif
  58. }
  59.  
  60. void
  61. CopyOne(BPTR inFile, const char *fileName)
  62. {
  63.     long    readCount, writeCount;
  64.     long    faultCode;
  65.  
  66.     do {
  67.         readCount = Read(inFile, buffer, BUFSIZE);
  68.         if (readCount > 0) {
  69.             writeCount = Write(myOutput, buffer, readCount);
  70.             if (writeCount != readCount) {
  71.                 faultCode = IoErr();
  72.                 SPrintf(errMsg,"Write error %ld on '%s'\n", faultCode, fileName);
  73.                 Abort(errMsg, faultCode);
  74.             }
  75.         }
  76.         else if (readCount < 0) {
  77.             faultCode = IoErr();
  78.             SPrintf(errMsg,"Read error %ld on '%s'\n", faultCode, fileName);
  79.             Abort(errMsg, faultCode);
  80.         }
  81.     } while (readCount > 0);
  82. }
  83.  
  84. void
  85. CopyNamedFile(const char *fileName)
  86. {
  87.     long    faultCode;
  88.     BPTR    inFile;
  89.  
  90.     inFile = Open(fileName, MODE_OLDFILE);
  91.     if (inFile == 0) {
  92.         faultCode = IoErr();
  93.         SPrintf(errMsg, "'%s' failed to open: %ld!\n", fileName, faultCode);
  94.         Abort(errMsg, faultCode);
  95.     }
  96.     CopyOne(inFile, fileName);
  97.     Close(inFile);
  98. }
  99.  
  100. main(int argc, char **argv)
  101. {
  102. #define NoMemory "Not enough memory!\n"
  103.  
  104.     int     argn;
  105.     int     fileCount;
  106.     int     fileIndex;
  107.     char    **fileList;
  108.  
  109. #ifdef ARP
  110.     ArpBase = (struct ArpBase *)OpenLibrary(ArpName, ArpVersion);
  111.     if (! ArpBase) exit(1);
  112. #endif
  113.  
  114.     progName = argv[0];
  115.     myOutput = Output();
  116.  
  117. #ifdef ARP
  118.     buffer = ArpAlloc(BUFSIZE);
  119. #else
  120.     buffer = malloc(BUFSIZE);
  121. #endif
  122.     if (!buffer) {
  123.         Write(myOutput,NoMemory, sizeof(NoMemory));
  124.         exit(1);    
  125.     }
  126.     if (argc < 2)
  127.         CopyOne(Input(), "Standard input");     /* Copy standard input. */
  128.     else for (argn = 1; argn < argc; ++argn) {
  129. #ifdef ARP
  130.         fileList = ArpFileScan(&argv[argn], 1, &fileCount, 1);
  131. #else
  132.         fileList = FileScan(&argv[argn], 1, &fileCount, 1);
  133. #endif
  134.         if (fileList != NULL) {
  135.             for (fileIndex = 0; fileIndex < fileCount; ++fileIndex) {
  136.                 CopyNamedFile(fileList[fileIndex]);
  137.             }
  138.         }
  139.     }
  140. }
  141.